/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

requirejs(['jquery', 'util', 'fancyBox', 'datatables', 'ztree', 'ztree_loader'], function($, util, fancyBox, dataTables, ztree, treeLoader) {
    $(function() {
        var plistPage = 0;
        var dT = false;
        fnAjaxLoadPds(plistPage);
        util.scrollBottom(function() {
            if ($('.search-w-box input').val() === '') {
                util.dataTableLoading();
                fnAjaxLoadPds(++plistPage);
            } else {
                scrolling = false;
            }
        }, 0);

        function pdDeleteListen() {
            $('.pd-del-btn').unbind('click').click(function() {
                var tR = $(this).parent().parent();
                if (confirm('你确定要删除这个产品吗')) {
                    $.post(shoproot + '?/WdminAjax/deleteProduct/', {
                        id: parseInt($(this).attr('data-product-id'))
                    }, function(res) {
                        if (parseInt(res) > 0) {
                            tR.fadeOut('normal', function() {
                                dT.row(tR).node().remove();
                            });
                        } else {
                            alert('删除失败');
                        }
                    });
                }
            });
        }

        function fnAjaxLoadPds(plistPage) {
            $.get(shoproot + '?/WdminPage/iframe_list_product/cat=' + $('#cat').val() + '&page=' + (plistPage) + '&part=n', function(html) {
                var json = html.toJson();
                if (json.count === 0) {
                    util.dataTableLoadingEnd('.dTable', true);
                    if (!dT) {
                        util.listEmptyTip();
                    }
                } else {
                    if (!dT) {
                        dT = $('.dTable').dataTable(DataTableConfig).api();
                    }
                    // dataTable加载数据
                    dT.rows.add(json.data).draw().nodes().to$().addClass('defTr font12 pdloaded' + plistPage).find('td:eq(2)').addClass('prices');
                    // loading结束
                    util.dataTableLoadingEnd('.dTable', false);
                    // hover事件监听
                    util.dataTableLis();
                    // 删除按钮监听
                    pdDeleteListen();
                }
            });
        }

    });
});